package bb.lanxing.view;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import bb.lanxing.R;
import bb.lanxing.util.DensityUtil;
import java.util.ArrayList;


public class LinearPercentView extends LinearLayout {
    private static Float CONNER_RADII = 10.0f;
    private static int DOT_WIDTH = 10;
    private static int TEXT_SIZE = 12;
    private LinearLayout llPercent;
    private LinearLayout llTitle;
    private ArrayList<PercentItem> percentItems;

    public LinearPercentView(Context context) {
        this(context, null);
    }

    public LinearPercentView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.percentItems = new ArrayList<>();
        initView(context);
    }

    public LinearPercentView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs);
    }

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.layout_linear_percent_view, this);
        this.llPercent = (LinearLayout) findViewById(R.id.ll_percent);
        this.llTitle = (LinearLayout) findViewById(R.id.ll_percent_title);
    }

    public void setPercentItems(ArrayList<PercentItem> percentItems) {
        this.percentItems.clear();
        this.percentItems.addAll(percentItems);
        refreshView();
    }

    private void refreshView() {
        this.llPercent.removeAllViews();
        this.llTitle.removeAllViews();
        for (int i = 0; i < this.percentItems.size(); i++) {
            addPercentItem(i);
            addTitleItem(i);
        }
    }

    private void addPercentItem(int i) {
        PercentItem percentItem = this.percentItems.get(i);
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setColor(getResources().getColor(percentItem.getColor()));
        if (i == 0) {
            gradientDrawable.setCornerRadii(new float[]{CONNER_RADII, CONNER_RADII, 0.0f, 0.0f, 0.0f, 0.0f, CONNER_RADII, CONNER_RADII});
        } else if (i == this.percentItems.size() - 1) {
            gradientDrawable.setCornerRadii(new float[]{0.0f, 0.0f, CONNER_RADII, CONNER_RADII, CONNER_RADII, CONNER_RADII, 0.0f, 0.0f});
        } else {
            gradientDrawable.setCornerRadii(new float[]{0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f});
        }
        View view = new View(getContext());
        view.setBackground(gradientDrawable);
        view.setLayoutParams(new LinearLayout.LayoutParams(0, -1, percentItem.getPercent().intValue()));
        this.llPercent.addView(view);
    }

    private void addTitleItem(int i) {
        PercentItem percentItem = this.percentItems.get(i);
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setColor(getResources().getColor(percentItem.getColor()));
        int i2 = DOT_WIDTH;
        gradientDrawable.setSize(i2, i2);
        gradientDrawable.setCornerRadius(90.0f);
        View view = new View(getContext());
        view.setBackground(gradientDrawable);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(DensityUtil.dp2px(12.0f), DensityUtil.dp2px(12.0f));
        layoutParams.setMargins(0, 0, DensityUtil.dp2px(10.0f), 0);
        view.setLayoutParams(layoutParams);
        TextView textView = new TextView(getContext());
        textView.setTextColor(getResources().getColor(R.color.grey_999999));
        textView.setTextSize(TEXT_SIZE);
        textView.setText(percentItem.getTitle());
        textView.setPadding(DensityUtil.dp2px(2.0f), 0, 0, 0);
        textView.setLayoutParams(new LinearLayout.LayoutParams(-2, -2));
        LinearLayout linearLayout = new LinearLayout(getContext());
        linearLayout.setOrientation(HORIZONTAL);
        linearLayout.setGravity(Gravity.CENTER_VERTICAL);
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(0, -1, 1.0f));
        linearLayout.addView(view);
        linearLayout.addView(textView);
        this.llTitle.addView(linearLayout);
    }

    public static class PercentItem {
        private int color;
        private Double percent;
        private String title;

        public PercentItem(int color, String title, Double percent) {
            this.title = title;
            this.color = color;
            this.percent = percent;
        }

        public String getTitle() {
            return this.title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public int getColor() {
            return this.color;
        }

        public void setColor(int color) {
            this.color = color;
        }

        public Double getPercent() {
            return this.percent;
        }

        public void setPercent(Double percent) {
            this.percent = percent;
        }
    }
}
